package com.brk.socket.controller;

import com.alibaba.fastjson.JSON;
import com.brk.socket.common.CommonResult;
import com.brk.socket.common.Regex;
import com.brk.socket.common.ResultCodeEnum;
import com.brk.socket.entity.Location;
import com.brk.socket.entity.LocationDO;
import com.brk.socket.entity.User;
import com.brk.socket.entity.UserDTO;
import com.brk.socket.server.websocket.WebSocketDispose;
import com.brk.socket.service.UserService;
import com.brk.socket.util.LocationInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/test")
    public CommonResult test() {
        return CommonResult.success("cjntsb");
    }

    @PostMapping("/importLocation")
    public CommonResult importLocation(@RequestBody Map<String, Object> map) {

        if (map == null || map.size() == 0 || map.get("polyX").toString().split(",").length <= 2) {
            return CommonResult.failed(ResultCodeEnum.ERROR_REQUEST_PARAM);
        }

        String polyX = map.get("polyX").toString();
        String polyY = map.get("polyY").toString();

        userService.importLocations(polyX, polyY);
        Location location = new Location();
        LocationInfoUtil.pareLocation(polyX, polyY, location);
        WebSocketDispose.setSetLocation(location);

        return CommonResult.success();
    }

    @GetMapping("/deleteLocation")
    public CommonResult deleteLocation() {
        userService.deleteLocation();
        return CommonResult.success();
    }

    @GetMapping("/getRecordLocation")
    public CommonResult getRecordLocation() {
        LocationDO locationDO = userService.getRecordLocation();

        if (locationDO != null) {
            return CommonResult.success(locationDO);
        }

        return CommonResult.failed(ResultCodeEnum.ERROR_POINT_NULL);
    }

    @PostMapping("/login")
    public CommonResult login(@RequestBody User user) {
        UserDTO userDTO = userService.login(user.getEmail(), user.getPassword());
        if (userDTO == null) {
            return CommonResult.failed(ResultCodeEnum.LOGIN_FAILURE);
        }

        return CommonResult.success(userDTO);

    }

    @PostMapping("/register")
    public CommonResult register(@RequestBody User user) {

        if (user.getEmail() == null || !user.getEmail().matches(Regex.IS_EMAIL)) {
            return CommonResult.failed(ResultCodeEnum.ERROR_REQUEST_PARAM);
        }

        if (userService.register(user) <= 0) {
            return CommonResult.failed(ResultCodeEnum.UNKNOWN_ERROR);
        }

        return CommonResult.success();

    }

    @GetMapping("/sendCode")
    public CommonResult sendCode(@RequestParam("email") String email) throws Exception {
        String verifyCode = userService.sendEmail(email);

        return CommonResult.success(verifyCode);
    }


    @PostMapping("/changePassword")
    public CommonResult changePassword(@RequestBody User user) {
        int result = userService.changPassword(user);
        if (result > 0) {
            return CommonResult.success();
        }
        return CommonResult.failed(ResultCodeEnum.USER_NOT_EXIT);
    }


}
